home *** CD-ROM | disk | FTP | other *** search
- Path: sun001.spd.dsccc.com!spd!jmccarty
- From: jmccarty@spd.dsccc.com (Mike McCarty)
- Newsgroups: comp.lang.c,comp.std.c,comp.lang.c++
- Subject: Re: Floating Point arithmetic problem
- Date: 15 Feb 1996 19:11:17 GMT
- Organization: DSC Communications Corporation, Plano, Texas USA
- Message-ID: <4g00gl$g83@sun001.spd.dsccc.com>
- References: <c968da6jzm.fsf@damayanti.india.ti.com>
- NNTP-Posting-Host: aplo139.spd.dsccc.com
-
- In article <c968da6jzm.fsf@damayanti.india.ti.com>,
- Kuntal Shah <kuntal@india.ti.com> wrote:
- )
- )I am having a wierd problem with floating point arithmetic. Gurus on
- )the net, please bail me out. I am working on the SUN 4.1.x platform.
- )
- )I have a "double" variable say d to which I need to add certain float
- )numbers of moderate magnitude (say less than 10000). This addition
- )occurs in a loop in my program which get executed more than a million
- )times depending on my testcase.
-
- [stuff cut]
-
- )This addition usually results in a few insignificant digits getting
- )accumulated in my double variable. For eg.
- )
- ) f = 12.99 is represented as 12.9900000000000002131628207
- ) f = 11.111 is represented as 11.1110000000000006536993169
- )
- )and so on. I am interested in comparisons with 4 digits precision.
- )
- )Now coming to the problem. The insignificant digits due to the
- )floating point representation keep accruing and there comes a stage
- )when the accrued value exceeds 0.0001 which results in failure of the
- )if condition in the above block of code, when ideally no such thing
- )should have occurred.
-
- [stuff cut]
-
- Every 1000 or 10000 or whatever, do a roundoff. Easiest way is:
-
- #define FACTOR 1e4
-
- d = floor(d*FACTOR+0.5)/FACTOR;
-
- Mike
- ----
- char *p="char *p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
-
- I don't speak for DSC. <- They make me say that.
-